package com.socialsoftware.model;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface ContactDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(Contact contact);
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<Contact> contacts);
    
    @Update
    void update(Contact contact);
    
    @Delete
    void delete(Contact contact);
    
    @Query("DELETE FROM contacts")
    void deleteAll();
    
    @Query("DELETE FROM contacts WHERE type = 0")
    void deleteNormalContacts();
    
    @Query("SELECT * FROM contacts WHERE type != 1 ORDER BY " +
           "CASE WHEN type = 2 THEN 0 " +  // Special contacts first
           "     WHEN isStarred = 1 THEN 1 " +  // Starred contacts second
           "     ELSE 2 END, " +  // Normal contacts last
           "firstLetter ASC, name ASC")
    LiveData<List<Contact>> getAllContacts();
    
    @Query("SELECT * FROM contacts WHERE type = 0 ORDER BY firstLetter ASC, name ASC")
    LiveData<List<Contact>> getNormalContacts();
    
    @Query("SELECT * FROM contacts WHERE isStarred = 1 ORDER BY name ASC")
    LiveData<List<Contact>> getStarredContacts();
    
    @Query("SELECT * FROM contacts WHERE type = 2 ORDER BY name ASC")
    LiveData<List<Contact>> getSpecialContacts();
    
    @Query("SELECT * FROM contacts WHERE type = 2 ORDER BY name ASC")
    List<Contact> getSpecialContactsSync();
    
    @Query("SELECT * FROM contacts WHERE name LIKE '%' || :searchQuery || '%' AND type != 1")
    LiveData<List<Contact>> searchContacts(String searchQuery);
    
    @Query("SELECT * FROM contacts WHERE id = :id")
    LiveData<Contact> getContactById(String id);
    
    @Query("SELECT * FROM contacts WHERE name = :name")
    LiveData<Contact> getContactByName(String name);
    
    @Query("SELECT COUNT(*) FROM contacts WHERE type = 0")
    int getContactCount();
    
    // 新增同步搜索方法，用于搜索好友
    @Query("SELECT * FROM contacts WHERE name LIKE :searchQuery OR id LIKE :searchQuery")
    List<Contact> searchContactsSync(String searchQuery);
} 